/**
 *  Copyright (C) 2008-2013  Telosys project org. ( http://www.telosys.org/ )
 *
 *  Licensed under the GNU LESSER GENERAL PUBLIC LICENSE, Version 3.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *          http://www.gnu.org/licenses/lgpl.html
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package org.telosys.util;

import java.io.IOException;
import java.io.Writer;


/**
 * Utility class to write standard Java objects as XML tag
 * 
 * @author Laurent Guerin
 *
 */
public class JavaObjectWriter
{
    private final static String END_OF_LINE = "\n" ;
        
    /**
     * Writes the given object in XML using the given Writer.<br>
     * Output : (tagname value="..." /)
     * @param writer
     * @param obj
     * @param sTagName
     * @return true if a tag has been generated, else false
     */
    public static boolean toXml(Writer writer, Object obj, String sTagName )
    {
        boolean bKnown = false ;
        if ( writer == null )
        {
            throw new IllegalArgumentException("JavaObjectWriter.toXml() : Writer is null");
        }
        if (obj != null)
        {
	        if (obj instanceof String)
	        {
	            bKnown = true ;
	            printValue( writer, sTagName, XmlUtil.xmlString( (String) obj ) );
			}
	        else if (obj instanceof java.lang.Number) // Byte, Short, Interger, Long, Double, Float, BigDecimal, BigInterger
	        { 
	            bKnown = true ;
	            printValue( writer, sTagName, ((java.lang.Number)obj).toString() );
			}
	        else if (obj instanceof java.lang.Boolean)
	        {
	            bKnown = true ;
	            printValue( writer, sTagName, ((java.lang.Boolean)obj).toString() );
			}
	        else if (obj instanceof java.util.Date)
	        {
	            bKnown = true ;
	            java.util.Date date = (java.util.Date)obj;
	            String sDateTime = DateUtil.dateTimeISO(date);
	            String sOther = " date=\"" + DateUtil.dateISO(date) + "\" time=\"" + DateUtil.timeISO(date) + "\" ";
	            printValue( writer, sTagName, sDateTime, sOther );
			}
	        //--- Print ...
	        if ( ! bKnown  )
	        {
	            printComment(writer, "Unknown object type '" + obj.getClass().getName() + "'" );
	        }
	        return bKnown ;
        }
        else
        {
            // object is null => pseudo 'null' value = '' 
	        printValue( writer, sTagName, "" ); 
	        return true ; // Unknown, but generated
        }
    }
    
    private static void printValue(Writer writer, String sTagName , String sXmlString )
    {
        printValue(writer, sTagName, sXmlString, null );
    }
    
    private static void printValue(Writer writer, String sTagName , String sXmlString, String sOtherAttributes )
    {
        String s = "<" + sTagName + " value=\"" + sXmlString + "\" " 
        		+ ( sOtherAttributes != null ? sOtherAttributes : "" )
        		+ "/>" ;
        
        try {
	        writer.write(s);
            writer.write(END_OF_LINE);
	        
	    } catch (IOException ex)
	    {
	        throw new RuntimeException("JavaObjectWriter.toXml() : IOException", ex);
	    }        
    }
    
    private static void printComment(Writer writer, String sComment )
    {
        try {
	        writer.write("<!-- " + sComment + " -->");
            writer.write(END_OF_LINE);
	        
	    } catch (IOException ex)
	    {
	        throw new RuntimeException("JavaObjectWriter.toXml() : IOException", ex);
	    }        
    }
}
